package com.koron.order.linepatrol.uav.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseBean;
import com.koron.bean.query.PageQuery;
import com.koron.order.linepatrol.uav.bean.VO.LineDefectPatrolUnv;
import com.koron.order.linepatrol.uav.bean.VO.LinePatrolUnv;
import com.koron.order.linepatrol.uav.bean.query.LineDefectPatrolUnvQuery;
import com.koron.order.linepatrol.uav.bean.query.LinePatrolUnvQuery;
import com.koron.order.linepatrol.uav.mapper.LineUnvDefectPatrolDetailMapper;
import com.koron.order.linepatrol.uav.mapper.LineUnvDefectPatrolMapper;
import com.koron.order.linepatrol.uav.service.LineUnvDefectPatrolService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: LineDefectPatrolServiceImpl
 * @Description:
 * @Date: 2023/2/9
 * @Author: 李一辰
 * @Version: 1.0
 **/
@Service
public class LineUnvDefectPatrolServiceImpl implements LineUnvDefectPatrolService {



    @Override
    @TaskAnnotation("list")
    public PageInfo<LineDefectPatrolUnv> list(SessionFactory sessionFactory, LineDefectPatrolUnvQuery patrolUnvQuery, PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getPageSize(),pageQuery.getOrderBy());
        LineUnvDefectPatrolMapper mapper = sessionFactory.getMapper(LineUnvDefectPatrolMapper.class);
        List<LineDefectPatrolUnv> list = mapper.list(patrolUnvQuery, pageQuery);
        List<String> ids = list.stream().map(BaseBean::getId).collect(Collectors.toList());
        LineUnvDefectPatrolDetailMapper detailMapper = sessionFactory.getMapper(LineUnvDefectPatrolDetailMapper.class);
        List<LinePatrolUnv> linePatrolUnvList = detailMapper.list(new LinePatrolUnvQuery());
        for (String id : ids) {
            long reportCount = linePatrolUnvList.stream().
                    filter(i -> id.equals(i.getDefectId())&&"1".equals(i.getDefectiveReportStatus()))
                    .count();
            long total = linePatrolUnvList.stream().
                    filter(i -> id.equals(i.getDefectId())).count();
            if(reportCount != 0 && reportCount == total){
                list.stream().filter(i->i.getId().equals(id)).forEach(i->i.setReportStatus("1"));
            }
            else if (reportCount >0) {
                list.stream().filter(i->i.getId().equals(id)).forEach(i->i.setReportStatus("3"));
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    @TaskAnnotation("update")
    public Boolean update(SessionFactory sessionFactory, LineDefectPatrolUnvQuery patrolUnvQuery) {
        LineUnvDefectPatrolMapper mapper = sessionFactory.getMapper(LineUnvDefectPatrolMapper.class);
        return mapper.update(patrolUnvQuery);
    }

    @Override
    @TaskAnnotation("deleteLogic")
    public Boolean deleteLogic(SessionFactory sessionFactory, String id) {
        LineUnvDefectPatrolMapper mapper = sessionFactory.getMapper(LineUnvDefectPatrolMapper.class);
        return mapper.deleteLogic(id);
    }

    @Override
    @TaskAnnotation("deleteBatch")
    public Integer deleteBatch(SessionFactory sessionFactory, List<String> ids) {
        LineUnvDefectPatrolMapper mapper = sessionFactory.getMapper(LineUnvDefectPatrolMapper.class);
        return mapper.deleteBatch(ids);
    }

    @Override
    @TaskAnnotation("insert")
    public Boolean insert(SessionFactory sessionFactory, LineDefectPatrolUnvQuery lineDefectPatrolUnv) {
        LineUnvDefectPatrolMapper mapper = sessionFactory.getMapper(LineUnvDefectPatrolMapper.class);
        return mapper.insert(lineDefectPatrolUnv);
    }
}
